package com.spring.nio.core.http;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author: YSFY
 * @date: 2021-02-21 13:30
 * @pacage_name: com.spring.nio.core.http
 * @class_name: HttpEnum
 * @description: Http 协议枚举
 */
public class HttpContext {

    public static SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.CHINA);

    public enum HttpParamEnum {
        HOST("Host", "请求的服务器主机名"),
        ACCEPT("Accept", "浏览器可支持的MIME类型。文件类型的一种描述方式。text/html,text/css,text/javascript,image/*"),
        REFERER("Referer", "浏览器通知服务器，当前请求来自何处。如果是直接访问，则不会有这个头。常用于：防盗链"),
        CONTENT_TYPE("Content-Type", "请求体类型，如果是POST请求，会有这个头，默认值为application/x-www-form-urlencoded，表示请求体内容使用url编码"),
        CONTENT_LENGTH("Content-Length", "请求体的长度"),
        ACCEPT_LANGUAGE("Accept-Language", "浏览器通知服务器，浏览器支持的语言。各国语言（国际化i18n）"),
        USER_AGENT("User-Agent", "浏览器通知服务器，客户端浏览器与操作系统相关信息"),
        ACCEPT_ENCODING("Accept-Encoding", "浏览器通知服务器，浏览器支持的数据压缩格式。如：GZIP压缩"),
        CONNECTION("Connection", "保持连接状态。Keep-Alive 连接中，close 已关闭"),
        CACHE_CONTROL("Cache-Control", "控制HTTP缓存"),
        COOKIE("Cookie", ""),

        DATE("Date", "日期"),
        OHC_RESPONSE_TIME("Ohc-Response-Time", ""),
        ETAG("ETag", ""),
        AGE("Age", ""),

        LOCATION("Location", "指定响应的路径，需要与状态码302配合使用，完成跳转。"),
        CONTENT_DISPOSITION("Content-Disposition", "通过浏览器以下载方式解析正文,取值：attachment;filename=xx.zip"),
        SET_COOKIE("Set-Cookie", "与会话相关技术。服务器向浏览器写入cookie"),
        CONTENT_ENCODING("Content-Encoding", "服务器使用的压缩格式取值：gzip"),
        REFRESH("Refresh", "定时刷新，格式：秒数;url=路径。url可省略，默认值为当前页。"),
        SERVER("Server", "指的是服务器名称，默认值：Apache-Coyote/1.1。可以通过conf/server.xml配置进,行修改"),
        EXPIRES("Expires", ""),
        PRAGMA("Pragma", ""),
        LAST_MODIFIED("Last-Modified", "服务器通知浏览器，文件的最后修改时间。与If-Modified-Since一起使用");
        private String param, description;
        HttpParamEnum(String param, String description) {
            this.param = param;
            this.description = description;
        }

        public String getParam() {
            return param;
        }

        public String getDescription() {
            return description;
        }
    }

    public static class HeaderEntry {

        private Map<String, Object> map = new HashMap<>();

        public void setHeader(String key, String value) {
            map.put(key, value);
        }

        public String getHeader(String key) {
            return String.valueOf(map.get(key));
        }

        public Map<String, Object> getMap() {
            return map;
        }

        @Override
        public String toString() {
            return "HeaderEntry{" +
                    "map=" + map.toString() +
                    '}';
        }
    }

    public class ResponseData {
        private String code;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        private String message;
    }
}
